package com.jutixeuyuan.service.impl;

import com.jutixeuyuan.service.CommentService;
import com.jutixueyuan.pojo.Comment;
import com.jutixueyuan.pojo.LivegoodsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public LivegoodsResult selectCommentById(String id) {

        try{
            Criteria criteria = Criteria.where("houseId").is(id);

            Query query = new Query(criteria);

            List<Comment> comments = mongoTemplate.find(query, Comment.class);

            for (Comment comment : comments) {
                String commentString = comment.getUsername().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                comment.setUsername(commentString);
            }


            return  LivegoodsResult.ok(comments);
        }catch (Exception e){
            return LivegoodsResult.error("请求失败");
        }
    }
}
